package cn.gcyl.module.biz.controller.admin.subscriptioninformation;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.gcyl.framework.common.pojo.PageParam;
import cn.gcyl.framework.common.pojo.PageResult;
import cn.gcyl.framework.common.pojo.CommonResult;
import cn.gcyl.framework.common.util.object.BeanUtils;
import static cn.gcyl.framework.common.pojo.CommonResult.success;

import cn.gcyl.framework.excel.core.util.ExcelUtils;

import cn.gcyl.framework.apilog.core.annotation.ApiAccessLog;
import static cn.gcyl.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.gcyl.module.biz.controller.admin.subscriptioninformation.vo.*;
import cn.gcyl.module.biz.dal.dataobject.subscriptioninformation.SubscriptionInformationDO;
import cn.gcyl.module.biz.service.subscriptioninformation.SubscriptionInformationService;

@Tag(name = "管理后台 - 订阅信息")
@RestController
@RequestMapping("/biz/subscription-information")
@Validated
public class SubscriptionInformationController {

    @Resource
    private SubscriptionInformationService subscriptionInformationService;

    @PostMapping("/create")
    @Operation(summary = "创建订阅信息")
//    @PreAuthorize("@ss.hasPermission('biz:subscription-information:create')")
    public CommonResult<Long> createSubscriptionInformation(@Valid @RequestBody SubscriptionInformationSaveReqVO createReqVO) {
        return success(subscriptionInformationService.createSubscriptionInformation(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新订阅信息")
//    @PreAuthorize("@ss.hasPermission('biz:subscription-information:update')")
    public CommonResult<Boolean> updateSubscriptionInformation(@Valid @RequestBody SubscriptionInformationSaveReqVO updateReqVO) {
        subscriptionInformationService.updateSubscriptionInformation(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除订阅信息")
    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('biz:subscription-information:delete')")
    public CommonResult<Boolean> deleteSubscriptionInformation(@RequestParam("id") Long id) {
        subscriptionInformationService.deleteSubscriptionInformation(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除订阅信息")
//                @PreAuthorize("@ss.hasPermission('biz:subscription-information:delete')")
    public CommonResult<Boolean> deleteSubscriptionInformationList(@RequestParam("ids") List<Long> ids) {
        subscriptionInformationService.deleteSubscriptionInformationListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得订阅信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('biz:subscription-information:query')")
    public CommonResult<SubscriptionInformationRespVO> getSubscriptionInformation(@RequestParam("id") Long id) {
        SubscriptionInformationDO subscriptionInformation = subscriptionInformationService.getSubscriptionInformation(id);
        return success(BeanUtils.toBean(subscriptionInformation, SubscriptionInformationRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得订阅信息分页")
//    @PreAuthorize("@ss.hasPermission('biz:subscription-information:query')")
    public CommonResult<PageResult<SubscriptionInformationRespVO>> getSubscriptionInformationPage(@Valid SubscriptionInformationPageReqVO pageReqVO) {
        PageResult<SubscriptionInformationDO> pageResult = subscriptionInformationService.getSubscriptionInformationPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SubscriptionInformationRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出订阅信息 Excel")
//    @PreAuthorize("@ss.hasPermission('biz:subscription-information:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportSubscriptionInformationExcel(@Valid SubscriptionInformationPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SubscriptionInformationDO> list = subscriptionInformationService.getSubscriptionInformationPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "订阅信息.xls", "数据", SubscriptionInformationRespVO.class,
                        BeanUtils.toBean(list, SubscriptionInformationRespVO.class));
    }

}